#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void StackInit(Stack* p)
{
	assert(p);
	p->capacity = 4;
	p->top = 0;
	p->data = (StackDataType*)malloc(sizeof(StackDataType) * p->capacity);
	if (p->data == NULL)
	{
		perror("malloc fail");
		return;
	}
}

void StackMemCre(Stack* p)
{
	assert(p);
	if (p->top >= p->capacity)
	{
		StackDataType* tmp = (StackDataType*)realloc(p->data, sizeof(StackDataType) * p->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		p->data = tmp;
		p->capacity *= 2;
	}
}

void StackPush(Stack* p, StackDataType x)
{
	StackMemCre(p);
	p->data[p->top] = x;
	p->top++;
}

void StackPop(Stack* p)
{
	assert(p);
	p->top--;
}

void StackDestory(Stack* p)
{
	assert(p);
	free(p->data);
}

bool StackEmpty(Stack* p)
{
	assert(p);
	return p->top == 0;
}

int StackSize(Stack* p)
{
	assert(p);
	return p->top;
}

StackDataType StackTop(Stack* p)
{
	return p->data[p->top - 1];
}